package cn.wolfcode.web.controller;

import cn.wolfcode.anno.RequirePermission;
import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    @RequirePermission(name = "部门查询",expression = "department:list")
    public String List(QueryObject qo, Model model){
        model.addAttribute("qo",qo);
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }

    @RequestMapping("/input")
    @ResponseBody
    @RequirePermission(name = "部门新增和修改",expression = "department:saveOrUpdate")
    public Department input(Long id){
        if (id != null){
            return departmentService.selectByPrimaryKey(id);
            //return JSON.toJSONString(department);
        }
        return null;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequirePermission(name = "部门新增和修改",expression = "department:saveOrUpdate")
    public String saveOrUpdate(Department department){
        if (department.getId() == null){
            departmentService.insert(department);
        }else {
            departmentService.updateByPrimaryKey(department);
        }
        return "redirect:/department/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "部门删除",expression = "department:delete")
    public String delete(Long id){
        departmentService.deleteByPrimaryKey(id);
        return "redirect:/department/list";
    }


}
